package com.tony.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: CountDownLatchDemo0011
 * @Auther: 皮蛋布丁
 * @Date: 2022/04/12/21:51
 * @Description: 倒计时门锁(没有公平和非公平机制，内部只创建了一个Sync子类，所以在该类的构造方法之中仅仅只是提供了一个人Sync(AQS子类)对象实例化)
 */
public class CountDownLatchDemo001 {

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(3); //总共的等待的数量为3
        for (int i = 0; i < 3; i++) {
            new Thread(()->{
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.printf("【%s】到达，并且已上车%n",Thread.currentThread().getName());
                countDownLatch.countDown(); //减1
            },"游客_" + i).start();
        }
        countDownLatch.await(); //等待计数为0后再解除阻塞
        System.out.println("【主线程】所有的旅客都到齐了，开车走人，去下一个景点购物消费");

    }
}
